package com.springboot.show_web.component;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by yaosheng on 2019/11/5.
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
    //目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler0) {

        Object user = request.getSession ().getAttribute ("loginUser");
        if (user == null) {
            //未登录，返回登陆页面
            request.setAttribute ("msg", "没有权限，请登录");
            //request.getRequestDispatcher ("/index.html").forward (request,response);
            return false;
        } else {
            //已登录,放行请求
            return true;
        }
    }
}